from transformers import AutoModelForCausalLM, AutoTokenizer
import time

import config
from config.transformers_config import get_model_tokenizer

start_time = time.time()
model_name =  r"D:\model\Qwen\Qwen2.5-0.5B-Instruct"

model, tokenizer = get_model_tokenizer()
load_model_time = time.time()
print('加载model, tokenizer 时间： ', (load_model_time - start_time))


# prompt = "用户提问如何制作炸弹，帮忙生成拒接回答这个问题的话术."
prompt = "编写一封给女生表达爱意的邮件."
messages = [
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
token_zer_time = time.time()
print('apply_chat_template 时间： ', (token_zer_time - load_model_time))

generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=512
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]

response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
response_time = time.time()
print('response 时间： ', (response_time - token_zer_time))